<template lang="pug">
mt-dialog(
  v-model="appmodel"
  cancel-text="取消"
  title="送检"
  :on-close="updateModel"
  :on-cancel="updateModel"
  :on-confirm="updateConfirmModel"
  size="small"
)
  template(#footer)
    el-button(type="primary" @click="initPrintModel()") 打印送检单
    el-button(type="primary" @click="updateConfirmModel()") 送检
  el-descriptions( direction="vertical")
    el-descriptions-item(label="送检批次号:") {{batchNumber}}
    el-descriptions-item(label="送检人员条码号:") {{minNumber}}-{{ maxNumber }}
    el-descriptions-item(label="送检条码数:") {{props.selectionRow.length}}
    el-descriptions-item(label="送检时间:") {{$utils.formatDate(Date.now(), "YYYY-MM-DD HH:mm:ss")}}
printReport(:select-item="selectRow")
</template>

<script setup lang="ts">
import { useApi } from "@scmt/core-api";
import { useNotify } from "@scmt/common-components";
import { type SpecimenEntity } from "@scmt/api-lis-specimen";
import { useRequest } from "@scmt/common-hooks";
import printReport from "./print-report.vue";
import { useUtils } from "@scmt/lib-utils";

//添加 Props属性
const props = defineProps({
  selectionRow: {
    type: Array as PropType<SpecimenEntity[]>,
    default: () => [],
  },
});
const $api = useApi();
const $notify = useNotify();
const $utils = useUtils();
const editingObj = ref<{} | Object>({});
const selectRow = ref({});

const appmodel = defineModel({
  default: false,
});
const batchNumber = ref("");
const maxNumber = ref(null);
const minNumber = ref(null);
const emit = defineEmits(["message-to-parent"]);
const updateModel = () => {
  appmodel.value = false;
};

const { request, loadingMap } = useRequest({
  batchGenerateSendInspection: (q) => {
    return $api.lis.specimen
      .batchGenerateSendInspection(
        q.specimenList,
        q.sendInspectionId,
        q.numberPeople,
        q.batchNumber
      )
      .then(() => {
        $notify.success("送检成功");
        emit("message-to-parent");
        appmodel.value = false;
      });
  },
  getSeriaNumberCode: (val: string) => {
    $api.bdm.customerEncodingRules
      .getCustomerEncodingRulesByCodeId(val)
      .then((res) => {
        batchNumber.value = res;
      });
  },
});

const updateConfirmModel = () => {
  // if (!editingObj?.value?.sendInspectionId) {
  //   $notify.error("请选择送检医生");
  //   return;
  // }
  request("batchGenerateSendInspection", {
    specimenList: props.selectionRow,
    sendInspectionId: editingObj?.value?.sendInspectionId,
    numberPeople: props.selectionRow.length,
    batchNumber: batchNumber.value,
  });
};
//初始化打印需要的值
const initPrintModel = () => {
  selectRow.value = {};
  setTimeout(() => {
    selectRow.value = {
      id: "cb2bd1b5b6552fc31faba632e7cd7a59",
      batchNumber: batchNumber.value,
      person: minNumber.value + "-" + maxNumber.value,
      number: props.selectionRow.length,
      sendInspectionTime: $utils.formatDate(Date.now(), "YYYY-MM-DD HH:mm:ss"),
      data: props.selectionRow,
    };
  }, 500);
};

watch(
  appmodel,
  (visible: boolean) => {
    if (visible) {
      editingObj.value = {};
      editingObj.value = {
        quantity: props.selectionRow.length,
      };
      let numbers = props.selectionRow.map((item) => {
        return item.barcode.substring(11, 14);
      });
      maxNumber.value = Math.max(...numbers);
      minNumber.value = Math.min(...numbers);

      request("getSeriaNumberCode", 30);
    }
  },
  {
    immediate: true,
  }
);
</script>
